package com.example.sparkmanager.controller;

import com.example.sparkmanager.dto.JobRequest;
import com.example.sparkmanager.dto.JobResponse;
import com.example.sparkmanager.dto.ScheduleRequest;
import com.example.sparkmanager.model.JobEntity;
import com.example.sparkmanager.model.JobStatus;
import com.example.sparkmanager.service.JobService;
import com.example.sparkmanager.service.SparkJobService;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/jobs")
@Slf4j
public class SparkJobController {
    @Autowired
    private SparkJobService jobService;
    @Autowired
    private JobService service;


    @GetMapping
    public String getAllJobs(Model model) {
        List<JobResponse> jobs = jobService.getAllJobs();
        model.addAttribute("jobs", jobs);
        return "index";
    }

    @GetMapping("/{id}")
    public String getJobById(@PathVariable Long id, Model model) {
        JobResponse job = jobService.getJobById(id);
        model.addAttribute("job", job);
        return "job-details";
    }

    @PostMapping
    public String createJob(@Valid JobRequest jobRequest) {
        jobService.createJob(jobRequest);
        return "redirect:/jobs";
    }

    @PostMapping("/{id}/schedule")
    public String updateJobSchedule(@PathVariable Long id, @Valid ScheduleRequest scheduleRequest) {
        jobService.updateJobSchedule(id, scheduleRequest);
        return "redirect:/jobs/" + id;
    }

    @GetMapping("/{id}/trigger")
    public String triggerJob(@PathVariable Long id) {
        jobService.triggerJob(id);
//        return "redirect:/jobs/" + id;
        return "redirect:/jobs";
    }

    @GetMapping("/{id}/cancel")
    public String cancelJob(@PathVariable Long id) {
        jobService.cancelJob(id);
        return "redirect:/jobs/" + id;
    }

    @GetMapping("/{id}/delete")
    public String deleteJob(@PathVariable Long id) {
        jobService.deleteJob(id);
        return "redirect:/jobs";
    }

    @GetMapping("/status/{status}")
    public String getJobsByStatus(@PathVariable String status, Model model) {
        List<JobResponse> jobs = jobService.getJobsByStatus(status);
        model.addAttribute("jobs", jobs);
        model.addAttribute("status", status);
        return "jobs-by-status";
    }


    @GetMapping("/checkJobStatus")
    public String checkJobStatus(Long jobId) {
        JobStatus jobStatus = jobService.checkJobStatus(jobId);
        log.info("当前任务的状态：{}", jobStatus);
        return "redirect:/jobs";
    }


    @GetMapping("/jobExecute")
    @ResponseBody
    public ResponseEntity<Void> jobExecute() {
        JobEntity job = new JobEntity();
        job.setId("xxm001");
        service.jobExecute(job);
        return ResponseEntity.noContent().build();
    }

    // REST API endpoints for integration
    @GetMapping("/api")
    @ResponseBody
    public List<JobResponse> getAllJobsApi() {
        return jobService.getAllJobs();
    }

    @GetMapping("/api/{id}")
    @ResponseBody
    public JobResponse getJobByIdApi(@PathVariable Long id) {
        return jobService.getJobById(id);
    }

    @PostMapping("/api")
    @ResponseBody
    public JobResponse createJobApi(@Valid @RequestBody JobRequest jobRequest) {
        return jobService.createJob(jobRequest);
    }

    @PutMapping("/api/{id}/schedule")
    @ResponseBody
    public JobResponse updateJobScheduleApi(@PathVariable Long id, @Valid @RequestBody ScheduleRequest scheduleRequest) {
        return jobService.updateJobSchedule(id, scheduleRequest);
    }

    @PostMapping("/api/{id}/trigger")
    @ResponseBody
    public JobResponse triggerJobApi(@PathVariable Long id) {
        return jobService.triggerJob(id);
    }

    @PostMapping("/api/{id}/cancel")
    @ResponseBody
    public JobResponse cancelJobApi(@PathVariable Long id) {
        return jobService.cancelJob(id);
    }

    @DeleteMapping("/api/{id}")
    @ResponseBody
    public ResponseEntity<Void> deleteJobApi(@PathVariable Long id) {
        jobService.deleteJob(id);
        return ResponseEntity.noContent().build();
    }

    @GetMapping("/api/status/{status}")
    @ResponseBody
    public List<JobResponse> getJobsByStatusApi(@PathVariable String status) {
        return jobService.getJobsByStatus(status);
    }
}